package cn.wowjoy.pda.android_pharmacy.ui.activity.door.take;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

import cn.wowjoy.pda.android_pharmacy.R;
import cn.wowjoy.pda.android_pharmacy.common.core.BaseActivity;
import cn.wowjoy.pda.android_pharmacy.common.utils.Contants;
import cn.wowjoy.pda.android_pharmacy.common.utils.NetWorkUtils;
import cn.wowjoy.pda.android_pharmacy.common.utils.PreferenceManager;
import cn.wowjoy.pda.android_pharmacy.common.widget.CreateDialog;
import cn.wowjoy.pda.android_pharmacy.databinding.ActivityDoorTakeIndexBinding;
import cn.wowjoy.pda.android_pharmacy.mvvm.presenter.DoorTakeIndexPresenter;
import cn.wowjoy.pda.android_pharmacy.mvvm.vm.DoorTakeIndexViewModel;
import cn.wowjoy.pda.android_pharmacy.ui.activity.door.merge.DoorMergeActivity;
import cn.wowjoy.pda.android_pharmacy.ui.activity.hospital.HandInputActivity;

public class DoorTakeIndexActivity extends BaseActivity<ActivityDoorTakeIndexBinding, DoorTakeIndexViewModel, DoorTakeIndexPresenter> implements  DoorTakeIndexPresenter.OnReceivePendingNum {
    //PDA扫码广播
    private ScanReceiver mScanReceiver;
    private IntentFilter intentFilter;
    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setViewModel(viewModel);
        binding.setPresenter(presenter);
        binding.doorIndexTitle.titleTextTv.setText("扫码取药");
        binding.doorIndexTitle.titleBackTv.setText("");
        binding.doorIndexTitle.titleBackLl.setVisibility(View.VISIBLE);
        //显示操作人姓名
        if(null != PreferenceManager.getInstance().getLUserName() || "".equals(PreferenceManager.getInstance().getLUserName())){
            binding.tvPharmacyCname.setText("工号: "+ PreferenceManager.getInstance().getLUserNum());
        }
        binding.doorIndexTitle.titleBackLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.doorIndexTitle.titleMenuConfirm.setVisibility(View.VISIBLE);
        binding.doorIndexTitle.titleMenuConfirm.setText("手动输入");
        binding.doorIndexTitle.titleMenuConfirm.setTextColor(getResources().getColor(R.color.white));
        binding.doorIndexTitle.titleMenuConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(DoorTakeIndexActivity.this, HandInputActivity.class);
                startActivityForResult(intent,Contants.REQUEST_CODE);
            }
        });
        presenter.registerOnReceivePendingNum(this);
        binding.btMerge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DoorTakeIndexActivity.this, DoorMergeActivity.class);
                startActivity(intent);
            }
        });
        mScanReceiver = new ScanReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction(Contants.WOW_NEW_ADD);

//        暂时替代广播
//        binding.imgvDoorIndexSaoma.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Contants.REQUEST_CODE:
                switch (resultCode){
                    case Contants.RESULT_HAND_SUCCESS_CODE:
                        //TODO:用户未操作完全的药单 按BACK键返回  刷新界面数据     暂时不考虑
                        String  handInput = data.getExtras().getString("handInput");
                        if(null != handInput && handInput.length()>1){
                            if(handInput.substring(0, 1).equalsIgnoreCase("C")){
                                String DJJLID=handInput.substring(1,handInput.indexOf("F"));
                                String BRDAID=handInput.substring(handInput.indexOf("F")+1,handInput.length());
                               // Log.e("TAG", "onReceive: "+DJJLID+" F "+BRDAID );
                                  presenter.queyCY("8", DJJLID,BRDAID);
                              //  presenter. loadDataCY(DJJLID,BRDAID);
                            }else{
                                presenter.quey("7", handInput);
                            }
                        }
                        break;
                    case Contants.RESULT_HAND_FAIL_CODE:

                        break;
                }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(mScanReceiver, intentFilter);
        //TODO:界面设置待审核流转单的数量
        presenter.setPendingNum("1");
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(mScanReceiver);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_door_take_index;
    }

    @Override
    protected Class<DoorTakeIndexPresenter> getPresenterClass() {
        return DoorTakeIndexPresenter.class;
    }

    @Override
    protected Class<DoorTakeIndexViewModel> getViewModelClass() {
        return DoorTakeIndexViewModel.class;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onPending(String onPendingNum) {
        binding.tvDoorIndexPendingNumber.setText(onPendingNum);
    }

    //TODO:接收PDA扫码广播，准备跳转
    class ScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Contants.WOW_NEW_ADD)) {
                    //要做下扫码的过滤判断
                    String stringExtra = intent.getStringExtra(Contants.WOW_NEW_KEY);
                    if(NetWorkUtils.isWifi(DoorTakeIndexActivity.this)){
                        if(null != stringExtra && stringExtra.length()>1){
                            if(stringExtra.substring(0, 1).equalsIgnoreCase("C")){
                                String DJJLID=stringExtra.substring(1,stringExtra.indexOf("F"));
                                String BRDAID=stringExtra.substring(stringExtra.indexOf("F")+1,stringExtra.length());
                            //Log.e("TAG", "onReceive: "+DJJLID+" F "+BRDAID );
                                presenter.queyCY("8", DJJLID,BRDAID);
                            }else{
                            //    presenter. loadData(stringExtra);
                                presenter.quey("7", stringExtra);
                            }
                        }
                    }else{
                        CreateDialog.failDialog3(DoorTakeIndexActivity.this,"错误","当前网络不可用，请检查你的网络设置");
                    }
                }
        }
    }


}

